package com.quarkboom.kotlinapp.intercept

/**
 * @author: zhuozhang6
 * @date: 2022/5/12
 * @email: zhuozhang6@iflytek.com
 */
//管理类
class RealInterceptorChain : InterceptorCallBack.Chain {

    private val mList: MutableList<InterceptorCallBack>? = ArrayList()
    private var index = 0
    private var lastResponse: String? = ""

    fun addLeaders(leader: InterceptorCallBack) {
        mList?.add(leader)
    }

    fun removeLeaders(leader: InterceptorCallBack) {
        mList?.remove(leader)
    }

    override fun proceed(request: String?): String? {
        lastResponse = request
        println("index的值：$index")
        if (mList == null || mList.size == 0) return lastResponse
        if (index >= mList.size) return lastResponse

        val nextIntIterator = mList[index]

        index++
        println("lastResponse的值:$lastResponse")

        lastResponse = nextIntIterator.intercept(this)
        return lastResponse
    }

    override fun getRequest(): String? {
        return lastResponse
    }
}